package com.castle.fortress.admin.contract.review.service.impl;

import cn.hutool.core.util.StrUtil;

import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.contract.review.entity.ContractReviewEntity;
import com.castle.fortress.admin.contract.review.dto.ContractReviewDto;
import com.castle.fortress.admin.contract.review.mapper.ContractReviewMapper;
import com.castle.fortress.admin.contract.review.service.ContractReviewService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;

/**
 * 合同审核记录表 服务实现类
 *
 * @author
 * @since 2025-04-23
 */
@Service
public class ContractReviewServiceImpl extends ServiceImpl<ContractReviewMapper, ContractReviewEntity> implements ContractReviewService {
    /**
    * 获取查询条件
    * @param contractReviewDto
    * @return
    */
    public QueryWrapper<ContractReviewEntity> getWrapper(ContractReviewDto contractReviewDto){
        QueryWrapper<ContractReviewEntity> wrapper= new QueryWrapper();
        if(contractReviewDto != null){
            ContractReviewEntity contractReviewEntity = ConvertUtil.transformObj(contractReviewDto,ContractReviewEntity.class);
            wrapper.like(contractReviewEntity.getReviewId() != null,"review_id",contractReviewEntity.getReviewId());
            wrapper.like(contractReviewEntity.getContractId() != null,"contract_id",contractReviewEntity.getContractId());
            wrapper.like(StrUtil.isNotEmpty(contractReviewEntity.getStance()),"stance",contractReviewEntity.getStance());
            wrapper.like(StrUtil.isNotEmpty(contractReviewEntity.getIntensity()),"intensity",contractReviewEntity.getIntensity());
            wrapper.like(StrUtil.isNotEmpty(contractReviewEntity.getRuleListId()),"rule_list_id",contractReviewEntity.getRuleListId());
            wrapper.like(StrUtil.isNotEmpty(contractReviewEntity.getReviewMethod()),"review_method",contractReviewEntity.getReviewMethod());
            wrapper.like(StrUtil.isNotEmpty(contractReviewEntity.getReviewStatus()),"review_status",contractReviewEntity.getReviewStatus());
            wrapper.like(contractReviewEntity.getReviewTime() != null,"review_time",contractReviewEntity.getReviewTime());
            wrapper.like(StrUtil.isNotEmpty(contractReviewEntity.getReviewResults()),"review_results",contractReviewEntity.getReviewResults());
            wrapper.like(StrUtil.isNotEmpty(contractReviewEntity.getReviewComments()),"review_comments",contractReviewEntity.getReviewComments());
            wrapper.like(contractReviewEntity.getCreateTime() != null,"create_time",contractReviewEntity.getCreateTime());
            wrapper.like(contractReviewEntity.getUpdateTime() != null,"update_time",contractReviewEntity.getUpdateTime());
            wrapper.like(contractReviewEntity.getCreateUser() != null,"create_user",contractReviewEntity.getCreateUser());
            wrapper.like(contractReviewEntity.getUpdateUser() != null,"update_user",contractReviewEntity.getUpdateUser());
        }
        return wrapper;
    }


    @Override
    public IPage<ContractReviewDto> pageContractReview(Page<ContractReviewDto> page, ContractReviewDto contractReviewDto) {
        QueryWrapper<ContractReviewEntity> wrapper = getWrapper(contractReviewDto);
        Page<ContractReviewEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<ContractReviewEntity> contractReviewPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<ContractReviewDto> pageDto = new Page(contractReviewPage.getCurrent(), contractReviewPage.getSize(),contractReviewPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(contractReviewPage.getRecords(),ContractReviewDto.class));
        return pageDto;
    }


    @Override
    public List<ContractReviewDto> listContractReview(ContractReviewDto contractReviewDto){
        QueryWrapper<ContractReviewEntity> wrapper = getWrapper(contractReviewDto);
        List<ContractReviewEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,ContractReviewDto.class);
    }
    
    @Override
    public ContractReviewEntity getByContractId(Long contractId) {
        if (contractId == null) {
            return null;
        }
        
        QueryWrapper<ContractReviewEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("contract_id", contractId);
        
        // 获取合同ID对应的最新审核记录
        return getOne(wrapper);
    }
}

